<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px" style="width: 80%; margin-left: 50px" :disabled="key">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="接口名称" prop="apiName">
            <el-input v-model="temp.apiName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调用者ip" prop="callerIp">
            <el-input v-model="temp.callerIp" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调用url" prop="callerUrl">
            <el-input v-model="temp.callerUrl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调用参数" prop="callerParams">
            <el-input v-model="temp.callerParams" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调用数据量" prop="callerSize">
            <el-input v-model="temp.callerSize" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调用耗时(ms)" prop="time">
            <el-input v-model="temp.time" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信息记录" prop="msg">
            <el-input v-model="temp.msg" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="temp.status" class="filter-item" placeholder="请选择状态">
              <el-option v-for="item in dictList('is_success')" :key="'type' + item.label" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <!-- <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus==='create'?createData():updateData()">
        {{ $t('table.confirm') }}
      </el-button> -->
      <el-button type="primary" @click="dialogFormVisible = false">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createMarketApiLog, updateMarketApiLog, getMarketApiLog } from '@/api/marketapilog/marketApiLog'

export default {
  name: 'MarketApiLogForm',
  data() {
    return {
      rules: {},
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建',
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      key: true,
    }
  },
  methods: {
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        apiName: undefined,
        callerId: undefined,
        callerIp: undefined,
        callerUrl: undefined,
        callerParams: undefined,
        callerDate: undefined,
        callerSize: undefined,
        time: undefined,
        msg: undefined,
        status: undefined,
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createMarketApiLog(this.temp).then((response) => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getMarketApiLog(id).then((response) => {
        if (response.data.code === 0) {
          this.temp = response.data.data
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updateMarketApiLog(this.temp).then((response) => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
  },
}
</script>
